from itsdangerous import TimedJSONWebSignatureSerializer as Serializer,SignatureExpired,BadSignature
from mall import settings

def generic_access_token(openid):
    #生成序列化器
    serializer = Serializer(settings.SECRET_KEY,3600)
    #组织数据
    data = {'openid': openid}

    #对敏感数据进行校验
    token = serializer.dumps(data)
    #返回数据
    return token.decode()

def check_access_token(token):
    #1创建序列化器
    serializer = Serializer(settings.SECRET_KEY,3600)
    #2.loads 操作会有异常
    try:
        result = serializer.loads(token)

    except BadSignature:
        return None
    else:
        return result.get('openid')